
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       LowPassFilter2p.h
  * @author     baiyang
  * @date       2021-11-7
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <gp_math/gp_mathlib.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
struct biquad_params{
    float cutoff_freq;
    float sample_freq;
    float a1;
    float a2;
    float b0;
    float b1;
    float b2;
};

typedef struct {
    float _delay_element_1;
    float _delay_element_2;
    bool initialised;
} DigitalBiquadFilterFloat;

typedef struct {
    Vector2f_t _delay_element_1;
    Vector2f_t _delay_element_2;
    bool initialised;
} DigitalBiquadFilterVector2f;

typedef struct {
    Vector3f_t _delay_element_1;
    Vector3f_t _delay_element_2;
    bool initialised;
} DigitalBiquadFilterVector3f;

typedef struct {
    struct biquad_params _params;
    DigitalBiquadFilterFloat _filter;
} LowPassFilter2pFloat;

typedef struct {
    struct biquad_params _params;
    DigitalBiquadFilterVector2f _filter;
} LowPassFilter2pVector2f;

typedef struct {
    struct biquad_params _params;
    DigitalBiquadFilterVector3f _filter;
} LowPassFilter2pVector3f;
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
//
void DigitalBiquadFilterFloat_ctor(DigitalBiquadFilterFloat *filter);
float DigitalBiquadFilterFloat_apply(DigitalBiquadFilterFloat *filter, float sample, const struct biquad_params *params);
void DigitalBiquadFilterFloat_reset(DigitalBiquadFilterFloat *filter);
void DigitalBiquadFilterFloat_reset2(DigitalBiquadFilterFloat *filter, float value, const struct biquad_params *params);
void DigitalBiquadFilter_compute_params(float sample_freq, float cutoff_freq, struct biquad_params *ret);

void LPF2pFloat_ctor(LowPassFilter2pFloat* lpf_2p_float);
void LPF2pFloat_ctor2(LowPassFilter2pFloat* lpf_2p_float, float sample_freq, float cutoff_freq);
void LPF2pFloat_set_cutoff_frequency(LowPassFilter2pFloat* lpf_2p_float, float sample_freq, float cutoff_freq);
static inline float LPF2pFloat_get_cutoff_freq(LowPassFilter2pFloat* lpf_2p_float) { return lpf_2p_float->_params.cutoff_freq;}
static inline float LPF2pFloat_get_sample_freq(LowPassFilter2pFloat* lpf_2p_float){ return lpf_2p_float->_params.sample_freq;}
float LPF2pFloat_apply(LowPassFilter2pFloat* lpf_2p_float, const float sample);
void LPF2pFloat_reset(LowPassFilter2pFloat* lpf_2p_float);
void LPF2pFloat_reset2(LowPassFilter2pFloat* lpf_2p_float, const float value);

//
void DBFVector2f_ctor(DigitalBiquadFilterVector2f *filter);
Vector2f_t DBFVector2f_apply(DigitalBiquadFilterVector2f *filter, const Vector2f_t* sample, const struct biquad_params *params);
void DBFVector2f_reset(DigitalBiquadFilterVector2f *filter);
void DBFVector2f_reset2(DigitalBiquadFilterVector2f *filter, const Vector2f_t* value, const struct biquad_params *params);

void LPF2pVector2f_ctor(LowPassFilter2pVector2f* lpf_2p_vector2);
void LPF2pVector2f_ctor2(LowPassFilter2pVector2f* lpf_2p_vector2, float sample_freq, float cutoff_freq);
void LPF2pVector2f_set_cutoff_frequency(LowPassFilter2pVector2f* lpf_2p_vector2, float sample_freq, float cutoff_freq);
static inline float LPF2pVector2f_get_cutoff_freq(LowPassFilter2pVector2f* lpf_2p_vector2) { return lpf_2p_vector2->_params.cutoff_freq;}
static inline float LPF2pVector2f_get_sample_freq(LowPassFilter2pVector2f* lpf_2p_vector2){ return lpf_2p_vector2->_params.sample_freq;}
Vector2f_t LPF2pVector2f_apply(LowPassFilter2pVector2f* lpf_2p_vector2, const Vector2f_t* sample);
void LPF2pVector2f_reset(LowPassFilter2pVector2f* lpf_2p_vector2);
void LPF2pVector2f_reset2(LowPassFilter2pVector2f* lpf_2p_vector2, const Vector2f_t* value);

//
void DBFVector3f_ctor(DigitalBiquadFilterVector3f *filter);
Vector3f_t DBFVector3f_apply(DigitalBiquadFilterVector3f *filter, const Vector3f_t* sample, const struct biquad_params *params);
void DBFVector3f_reset(DigitalBiquadFilterVector3f *filter);
void DBFVector3f_reset2(DigitalBiquadFilterVector3f *filter, const Vector3f_t* value, const struct biquad_params *params);

void LPF2pVector3f_ctor(LowPassFilter2pVector3f* lpf_2p_vector3);
void LPF2pVector3f_ctor2(LowPassFilter2pVector3f* lpf_2p_vector3, float sample_freq, float cutoff_freq);
void LPF2pVector3f_set_cutoff_frequency(LowPassFilter2pVector3f* lpf_2p_vector3, float sample_freq, float cutoff_freq);
static inline float LPF2pVector3f_get_cutoff_freq(LowPassFilter2pVector3f* lpf_2p_vector3) { return lpf_2p_vector3->_params.cutoff_freq;}
static inline float LPF2pVector3f_get_sample_freq(LowPassFilter2pVector3f* lpf_2p_vector3){ return lpf_2p_vector3->_params.sample_freq;}
Vector3f_t LPF2pVector3f_apply(LowPassFilter2pVector3f* lpf_2p_vector3, const Vector3f_t* sample);
void LPF2pVector3f_reset(LowPassFilter2pVector3f* lpf_2p_vector3);
void LPF2pVector3f_reset2(LowPassFilter2pVector3f* lpf_2p_vector3, const Vector3f_t* value);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



